﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChurchData.Business
{
    public class LKAssociationBO : ChurchDataBusinessBase, ILKTable
    {
        public List<GenericLKTableResults> GetActiveRecords()
        {
            return this.GetActiveRecords(null);
        }

        public List<GenericLKTableResults> GetActiveRecords(int? grandFatherPK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            var x =
                    from oLKA in oDB.LKAssociations
                    where oLKA.ActiveInd == true || (grandFatherPK != null && grandFatherPK == oLKA.LKAssociationPK)
                    select oLKA;
            
            List<GenericLKTableResults> oList = new List<GenericLKTableResults>();
            foreach (LKAssociation oLK in x)
            {
                GenericLKTableResults oGLK = new GenericLKTableResults();
                oGLK.Pk = oLK.LKAssociationPK;
                oGLK.Description = oLK.DescriptionText;
                oList.Add(oGLK);
            }
            return oList;
        }

        public int InsertRecord(string description)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                LKAssociation oLKA = new LKAssociation();
                oLKA.ActiveInd = true;
                oLKA.DescriptionText = description;
                oLKA.LastUpdUser = UserInformation.UserName;
                oLKA.LastUpdTimestamp = DateTime.Now;
                oDB.LKAssociations.InsertOnSubmit(oLKA);
                oDB.SubmitChanges();
                return oLKA.LKAssociationPK;
            }
        }

        public bool ModifyRecord(int pk, string description)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x = (from oLKA in oDB.LKAssociations
                         where oLKA.LKAssociationPK == pk
                         select oLKA).Single();
                x.DescriptionText = description;
                x.LastUpdUser = UserInformation.UserName;
                x.LastUpdTimestamp = DateTime.Now;
                oDB.SubmitChanges();
            }

            return true;
        }


        public string GetDescriptionByPK(int pk)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            var x =
                    from oLKA in oDB.LKAssociations
                    where oLKA.LKAssociationPK == pk
                    select oLKA.DescriptionText;
            List<string> oList = x.ToList();
            return (oList.Count == 0) ? "" : oList[0];
        }

        public bool SetToInactive(int pk)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x = (from oLKA in oDB.LKAssociations
                         where oLKA.LKAssociationPK == pk
                         select oLKA).Single();
                x.ActiveInd = false;
                x.LastUpdUser = UserInformation.UserName;
                x.LastUpdTimestamp = DateTime.Now;
                oDB.SubmitChanges();
            }

            return true;
        }
    }//class
}
